package kargo.suratkargokargotakip;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	DataBase DataBase;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DataBase = new DataBase(this.getApplicationContext());
		try {
			KayitGoster(KayitGetir());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}

		Button btnKargoTakip = (Button) findViewById(R.id.btnKargoTakip);

		btnKargoTakip.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, KargoTakipSecenekleri.class);

		TextView textView = (TextView) findViewById(R.id.editText1);

		String textBoxStr = textView.getText().toString();

		intent.putExtra("CariKodu", textBoxStr);

		try {
			SQLiteDatabase db = DataBase.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put("CariKodu", textBoxStr);

			db.insertOrThrow("CariTable", null, cv);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		startActivity(intent);
	}

	private Cursor KayitGetir() {
		SQLiteDatabase db = DataBase.getReadableDatabase();
		Cursor c = db.query("CariTable", new String[] { "Id", "CariKodu" },
				null, null, null, null, null);
		startManagingCursor(c);
		return c;
	}

	protected void KayitGoster(Cursor crs) {
		while (crs.moveToNext()) {
			((TextView) findViewById(R.id.editText1)).setText(crs.getString(crs
					.getColumnIndex("CariKodu")));

		}
	}

}
